/****************************************************************************
**
** MultiMap.cpp
**
** Copyright (C) August 2014 Hotride
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*****************************************************************************
*/
//----------------------------------------------------------------------------
#include "stdafx.h"
//----------------------------------------------------------------------------
TMultiMap::TMultiMap(string path)
{
	m_Width = 0;
	m_Height = 0;
	memset(&m_Texture, 0, sizeof(m_Texture));

	FILE *file = fopen(path.c_str(), "rb");
	if (file != NULL)
	{
		fread(&m_Width, 4, 1, file);
		fread(&m_Height, 4, 1, file);

		if (m_Width < 1 || m_Height < 1)
		{
			TPRINT("Failed to load bounds from MultiMap.rle\n");
			m_Width = 0;
			m_Height = 0;
		}
		else
		{
			m_Texture.Width = m_Width;
			m_Texture.Height = m_Height;

			PDWORD m_Map = new DWORD[m_Width * m_Height * 4];

			int x = 0;
			int y = 0;

			DWORD data_color = 0x004284A5;
			data_color = (0xFF << 24) | (GetBValue(data_color) << 16) | (GetGValue(data_color) << 8) | GetRValue(data_color);

			while (!feof(file))
			{
				BYTE pic = 0;
				fread(&pic, 1, 1, file);

				BYTE size = pic & 0x7F;

				DWORD color = 0;
				if (pic & 0x80) color = data_color;

				IFOR(i, 0, size)
				{
					m_Map[(y * m_Width) + x] = color;
					x++;

					if (x >= m_Width)
					{
						x = 0;
						y++;
					}

					if (y >= m_Height) break;
				}

				if (y >= m_Height) break;
			}

			UO->m_GL.BindTexture(m_Texture.Texture, m_Width, m_Height, m_Map);

			delete m_Map;
		}

		fclose(file);
	}
	else {TPRINT("Failed to load MultiMap.rle\n");}
}
//----------------------------------------------------------------------------
TMultiMap::~TMultiMap()
{
	if (!m_Texture.Empty()) m_Texture.Clear();
}
//----------------------------------------------------------------------------